package top.magicpotato.fast.quartz.listener;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.magicpotato.fast.quartz.exception.JobNotExisting;

/**
 * @author ycl
 */
public class QuartzJobListener implements JobListener {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String getName() {
        return getClass().getName();
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {

    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {

    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        try {
            if(jobException instanceof JobNotExisting){
                logger.error("",jobException);
                context.getScheduler().deleteJob(context.getJobDetail().getKey());
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
